/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import java.util.Iterator;

import org.eclipse.emf.common.util.EList;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Stereotype;

import com.cea.papyrus.core.helper.IGraphElementHelper;
import com.cea.papyrus.core.utils.Debug;

/**
 * Generic command to delete element in the model, with no information about current diagram.
 * 
 * @author Patrick Tessier
 * @deprecated use Removecommand based on EMF
 */
public class UMLElementDeleteCommand extends PapyrusCommand {
	
	/**
	 * Create element.
	 */
	private Element element;
	
	/**
	 * parent of the created element.
	 */
	private Element parent;
	
	/**
	 * The umlchild.
	 */
	private Element umlchild;
	
	// stereotypes
	/**
	 * 
	 */
	private EList stereotypes;
	
	/**
	 * Default Constructor.
	 * 
	 * @param elementClass the type of element to create
	 * @param umlparent the umlparent
	 * @param umlelement the umlelement to delete
	 */
	public UMLElementDeleteCommand(Element umlelement, Element umlparent) {
		super(umlelement.getClass().getInterfaces()[0]);
		this.parent = umlparent;
		this.umlchild=umlelement;
		this.setLabel("\"Deletion of "+ elementClass.getSimpleName()+"\"");
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	/**
	 * 
	 */
	public void execute() {
		Debug.debug(this, "execute "+getLabel(), channel);
		
		// should unapply stereotypes.
		
		stereotypes = umlchild.getAppliedStereotypes();
		Iterator it = umlchild.getAppliedStereotypes().iterator();
		while(it.hasNext()) {
			Stereotype st = (Stereotype) it.next();
			umlchild.unapplyStereotype(st);
		}
		if(elementFactory instanceof IGraphElementHelper) {
			((IGraphElementHelper)elementFactory).removeUMLElement(umlchild, parent);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	public boolean canExecute() {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canUndo()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	public boolean canUndo() {
		return true;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	/**
	 * 
	 */
	public void undo() {
		if(elementFactory instanceof IGraphElementHelper) {
			((IGraphElementHelper)elementFactory).addUMLElement(umlchild, parent);
		} 
		
		// reapply unapplied stereotypes
		Iterator it = stereotypes.iterator();
		while(it.hasNext()) {
			Stereotype st = (Stereotype) it.next();
			umlchild.applyStereotype(st);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	/**
	 * 
	 */
	public void redo() {
		stereotypes = umlchild.getAppliedStereotypes();
		
		Iterator it = umlchild.getAppliedStereotypes().iterator();
		while(it.hasNext()) {
			Stereotype st = (Stereotype) it.next();
			umlchild.unapplyStereotype(st);
		}
		
		if(elementFactory instanceof IGraphElementHelper) {
			((IGraphElementHelper)elementFactory).removeUMLElement(umlchild, parent);
		}
	}

	/**
	 * Returns the created element or <code>null</code>.
	 * 
	 * @return the created element
	 */
	public Element getElement() {
		return element;
	}
}
